﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MaxLeaf.Common;
using MaxLeaf.DB;

namespace MaxLeaf.DAL.Web
{
    /// <summary>
    /// 类别
    /// </summary>
    public class mary_t_SubMessageDAL : BaseDAL
    {
        public mary_t_SubMessageDAL()
            : base()
        {
            strTableName = "mary_t_SubMessage";
            strKeyFieldName = "FInterID";
            strFieldNames = "FID,FParentID,FName,FTypeID,FDetail,FDeleted";
        }
        protected override DataTable DAL_GetData(DBAccess dbOrgCon, DBAccess destCon, int intdataType, object objParam)
        {
            GetDataType dataType = (GetDataType)intdataType;

            if (dataType == GetDataType.OrgData)
            {
                return Gett_SubMessage(dbOrgCon, CvtFunc.ToString(objParam));
            }

            return base.DAL_GetData(dbOrgCon, destCon, intdataType, objParam);
            
        }

        protected override void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FParentID", StringComparison.InvariantCultureIgnoreCase))
            {

                drDestData[strFieldName] = GetFParentID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FParentID"]));

                
            }
            else
            {
                base.DAL_GetFieldData(dbOrgCon, dbDestCon, strFieldName, drOrgData, drDestData);
            }
        }

        private string  GetFParentID(DBAccess dbOrgCon, DBAccess dbDestCon, string  strOrgTypeID)
        {
            DataRow drOrg = dbOrgCon.GetTableDataFirstRow("t_SubMesType", new Dictionary<string, object> { { "FTypeID", strOrgTypeID } });
            Updatemary_t_SubMesType(dbDestCon, drOrg);

            return strOrgTypeID;
        }

        private void Updatemary_t_SubMesType(DB.DBAccess dbCon, DataRow drOrg)
        {
            string strKeyFieldName = "FTypeID";
            string strTableName = "mary_t_SubMesType";
            string strFieldNames = "FName,FDetail,FType";

            string strKeyValue = CvtFunc.ToString(drOrg["FTypeID"]);
            DataTable dtData = dbCon.GetTableData(strTableName, new Dictionary<string, object> { { strKeyFieldName, strKeyValue } }, strKeyFieldName + "," + strFieldNames);
            DataRow drData = null;
            LogStatus logStatus = LogStatus.Update;
            if (dtData.Rows.Count == 0)
            {
                logStatus = LogStatus.Add;
                drData = dtData.NewRow();
                drData[strKeyFieldName] = strKeyValue;

                strFieldNames = strKeyFieldName + "," + strFieldNames;
            }
            else
            {
                drData = dtData.Rows[0];
            }

            string[] arrFieldName = strFieldNames.Split(',');
            foreach (string strField in arrFieldName)
            {
                string strOrgField = strField;


                drData[strField] = drOrg[strOrgField];
            }

            KeyValuePair<string, IDataParameter[]> kvp = dbCon.CreateParamSql(strFieldNames, strTableName, drData, logStatus, strKeyFieldName);

            dbCon.ExecuteSql(kvp.Key, kvp.Value);
        }

        
        private DataTable Gett_SubMessage(DBAccess dbOrgCon, string strFItemID)
        {

            DataTable dtt_SubMessage = null;
            if (dtt_SubMessage == null)
            {
                dtt_SubMessage = dbOrgCon.GetTableData("t_SubMessage", new Dictionary<string, object> { { "FInterID", strFItemID } });

            }
            return dtt_SubMessage;
        }

        internal string  GetFInterID(DBAccess dbOrgCon, DBAccess dbDestCon, string strFInterID)
        {
            if (strFInterID.Equals("0") || strFInterID.Length == 0)
            {
                return "0";
            }
            Update(dbOrgCon, dbDestCon, strFInterID);

            return strFInterID;
        }
    }

    
}
